package com.ken.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "DeleteServlet", urlPatterns = {"/deleteservlet"})
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
//        String username = request.getParameter("username");

//        System.out.println("接受到的POST请求参数 Cpage username："+username);
        response.setContentType("text/html;charset = utf-8");

        Cookie cookie = new Cookie("username", null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);

        StringBuffer stf = new StringBuffer();

        PrintWriter out = response.getWriter();
        stf.append("<!DOCTYPE html>");
        stf.append("<html lang=\"en\">");
        stf.append("<head>");
        stf.append("<meta charset=\"UTF-8\">");
        stf.append(" <title>删除Cookie页面</title>");
        stf.append("</head>");
        stf.append("<body>");
        stf.append("<h3 >Cookie已经删除</h3>");
//        stf.append(" Cookie已经删除  用户名：").append(username).append("<br>");
        stf.append(" </body>");
        stf.append(" </html>");

        out.print(stf.toString());
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
